package logic

import (
	"context"
	"errors"
	"srv/models/mysql"
	"srv/models/redis"

	"srv/internal/svc"
	"srv/tiktok"

	"github.com/zeromicro/go-zero/core/logx"
)

type TiktokAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTiktokAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TiktokAddLogic {
	return &TiktokAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TiktokAddLogic) TiktokAdd(in *tiktok.TiktokAddRequest) (*tiktok.TiktokAddResponse, error) {
	// todo: add your logic here and delete this line
	m := mysql.Mail{}
	err := m.Info(int(in.MailId))
	if err != nil {
		logx.Error("商品不存在")
		return nil, err
	}
	if m.ID == 0 {
		logx.Error("商品不存在")
		return nil, errors.New("商品不存在")
	}
	if m.Stock < int(in.Num) {
		logx.Error("商品库存不足")
		return nil, errors.New("商品库存不足")
	}
	redis.MailAdd(int(in.MailId), int(in.Num), int(in.UserId), m.Size, m.Title, m.Price)
	return &tiktok.TiktokAddResponse{}, nil
}
